package pl.bizuteria.sklep.admin.client.ioc;

import pl.bizuteria.sklep.admin.client.activity.mapper.AppActivityMapper;
import pl.bizuteria.sklep.admin.client.activity.mapper.ProductActivityMapper;
import pl.bizuteria.sklep.admin.client.ui.ITwoSlotView;
import pl.bizuteria.sklep.admin.client.ui.TwoSlotView;
import pl.bizuteria.sklep.admin.client.ui.product.earing.EaringProxyListView;
import pl.bizuteria.sklep.admin.client.ui.simple.TopBarView;
import pl.bizuteria.sklep.api.shared.request.ApplicationRequestFactory;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.inject.client.AbstractGinModule;
import com.google.gwt.place.shared.PlaceController;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;

public class DesktopModule extends AbstractGinModule {

	@Override
	protected void configure() {
		bind(PlaceController.class).toProvider(PlaceControllerProvider.class)
				.in(Singleton.class);
		bind(ApplicationRequestFactory.class).toProvider(
				RequestFactoryProvider.class).in(Singleton.class);
		bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class);

		bind(ITwoSlotView.class).to(TwoSlotView.class).in(Singleton.class);

		bind(AppActivityMapper.class).in(Singleton.class);
		bind(ProductActivityMapper.class).in(Singleton.class);
		bind(EaringProxyListView.class).in(Singleton.class);
		bind(TopBarView.class).in(Singleton.class);

		// nested element in ui - in many views -> cannot be singleton
		// bind(ProductButtonView.class);
	}

	static class PlaceControllerProvider implements Provider<PlaceController> {

		private final EventBus eventBus;

		@Inject
		public PlaceControllerProvider(EventBus eventBus) {
			this.eventBus = eventBus;
		}

		@Override
		public PlaceController get() {
			return new PlaceController(eventBus);
		}

	}

	static class RequestFactoryProvider implements
			Provider<ApplicationRequestFactory> {
		private final ApplicationRequestFactory requestFactory;

		@Inject
		public RequestFactoryProvider(EventBus eventBus) {
			requestFactory = GWT.create(ApplicationRequestFactory.class);
			requestFactory.initialize(eventBus);
		}

		@Override
		public ApplicationRequestFactory get() {
			return requestFactory;
		}
	}
}